package com.atguigu.flink.utils;

import org.apache.flink.streaming.api.windowing.windows.TimeWindow;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Smexy on 2023/4/7
 */
public class MyUtil
{
    /*
        把 Iterable<T> elements 转换为List
     */
    public static <T> List<T> parseToList(Iterable<T> elements){

        List<T> result = new ArrayList<>();

        for (T element : elements) {
            result.add(element);
        }

        return result;

    }

    private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    /*
            把long ts转换为时间格式
     */
    public static String parseTimeWindow(TimeWindow w){

        String startTimeStr = LocalDateTime.ofInstant(Instant.ofEpochMilli(w.getStart()), ZoneId.of("Asia/Shanghai")).format(formatter);
        String endTimeStr = LocalDateTime.ofInstant(Instant.ofEpochMilli(w.getEnd()), ZoneId.of("Asia/Shanghai")).format(formatter);

        return "[" + startTimeStr + "," + endTimeStr + ")";

    }
}
